{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4cbb803e-0304-4380-b089-84cf3bf10d1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from MobileNetV2 import mobilenet_v2\n",
    "from torchvision import transforms\n",
    "from torchvision import datasets\n",
    "from torch.utils.data import DataLoader\n",
    "import numpy as np\n",
    "from MobileNetV3 import mobilenetv3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b06883ee-7310-45a6-bd93-a564bd259e69",
   "metadata": {},
   "outputs": [],
   "source": [
    "save_path = 'modelv3.pth'\n",
    "model = mobilenetv3(mode=\"large\")\n",
    "# model = mobilenet_v2()\n",
    "model.load_state_dict(torch.load(save_path))\n",
    "model = model.cuda(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3806c816-cc40-4869-a1ba-00dc62cd1c0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 超参数\n",
    "input_size = 224\n",
    "batch_size = 27\n",
    "n_worker = 8\n",
    "gpu_id = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6ca501ba-424f-4e19-baef-8ebffb1bfbe6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入数据transform\n",
    "data_transform = transforms.Compose([\n",
    "        # transforms.RandomResizedCrop(input_size, scale=(0.2, 1.0)),\n",
    "        # transforms.Resize([input_size, input_size]),\n",
    "        transforms.ToTensor(),\n",
    "])\n",
    "test_path = \"image/test_image\"\n",
    "test_dataset = datasets.ImageFolder(test_path, transform=data_transform)\n",
    "test_loader = DataLoader(\n",
    "    test_dataset, batch_size=batch_size, shuffle=False, \n",
    "    num_workers=n_worker, pin_memory=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "08becd83-af83-4085-b805-ab9fa8325b86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.45"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ans = 0\n",
    "idx = 0\n",
    "model.eval()\n",
    "with torch.no_grad():\n",
    "    for data, label in test_loader:\n",
    "        data, label = data.cuda(2), label.cuda(2)\n",
    "        output = model(data)\n",
    "        preds = torch.argmax(output, 1)\n",
    "        unique_values, counts = torch.unique(preds, return_counts=True)\n",
    "        resp = unique_values[counts.argmax()]\n",
    "\n",
    "        if resp.item() == idx:\n",
    "            # print(f'person {idx} identify success!')\n",
    "            ans += 1\n",
    "            # print(f'person {idx} identify fail')\n",
    "        idx += 1\n",
    "ans / 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "532a9f09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7449fbd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
